<?php
defined('SYSPATH') or die('No direct script access.');


class Controller_Notify extends Controller{

public function action_index()
{
    if(isset($_POST["no"]) && $_POST["no"] != ""){
        $out_trade_no = $_POST["no"];
        require Kohana::find_file("vendor/wechat","lib/WxPay.Api");
        $input = new WxPayOrderQuery();
        $input->SetOut_trade_no($out_trade_no);
        $value=WxPayApi::orderQuery($input);
        if($value["return_code"]=="SUCCESS" && $value["result_code"]=="SUCCESS")
        {
            if($value["trade_state"]=="SUCCESS")
            {
                $buy=ORM::factory("buy",array("order_id"=>$out_trade_no));
                if(!$buy->loaded())
                {
                    $buy->product_id=$value["attach"];
                    $buy->order_id=$out_trade_no;
                    $buy->user_id=ORM::factory("user",array("user_shopid"=>Session::instance()->get("user")["sid"]))->user_id;
                    $buy->buy_price=$value["total_fee"];
                    $buy->buy_time=$value["time_end"];
                    $buy->save();
                }
                echo "true";
            }
           else
           {
               echo "false";
           }
        }
        else
        {
            echo "false";
        }
    }
    else
    {
        echo "false";
    }
}

}
?>